package com.supplychainapi.common.enums;


import com.joindata.inf.common.basic.exceptions.ServiceException;

/**
 * Created by wangziqi on 2017/9/20.
 */
public enum ExceptionMessage {

    is_activate(1, "启用"),
    system_error(50000, "系统异常"),
    missing_param(50001, "参数为空"),
    invalid_param(50002, "非法参数"),
    invalid_user(50003, "非法用户"),
    invalid_token(50004, "token无效"),
    unauthorized_role(50005, "角色无权限"),
    error_password(50006, "用户名或密码错误"),
    bind_none_stores(50007, "该账户还未绑定任何店铺"),
    no_permissions(50008, "没有权限"),
    no_query(50009, "未查到数据"),
    sign_error(500010, "签名错误"),
    role_error(500011, "请使用服务员账号登录"),
    template_error(500012, "模板错误"),
    operation_failed(500013, "操作失败"),
    code_error(500014, "编码小于库中最大值,请修改后重试"),
    tree_type_lv_error(500015, "分类类型,大于最大级别"),
    material_name_is_exit(500016, "名称已存在"),
    delete_treebuild_failed(500017, "该数据有被引用不能删除"),
    delete_failed(500018, "该分类不是末级分类,请从末级分类开始操作"),
    code_error_param(500019, "代码不能含非法字符及中文,只能为数字加字母形式"),
    emoji_failed(500020, "名称不能含有emoji表情"),
    code_same(500021, "编码重复请重试"),
    auto_code_format(500022, "编码格式为字母加数字允许有_-."),
    address_nofound(500023, "未查询到该地址"),
    address_isExit(500024, "地址已存在");



    public static ExceptionMessage getByCode(Integer code) {
        if (null != code) {
            for (ExceptionMessage e : values()) {
                if (code.equals(e.getCode()) || e.getCode() == code) {
                    return e;
                }
            }
        }
        return null;
    }

    private int code;

    private String msg;

    ExceptionMessage(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public static ExceptionMessage getByServiceException(ServiceException e) {
        return getByCode(e.getCode());
    }


    public ServiceException getServiceException() {
        return new ServiceException(code, msg);
    }

    public static ServiceException getSystemException(Exception e) {
        return new ServiceException(500, e);
    }


}
